import { Component, ElementRef, Injector, OnChanges, OnInit, SimpleChanges, ViewChild} from '@angular/core';
import { ActivatedRoute, NavigationEnd, Params, Router } from '@angular/router';
import { filter, map } from 'rxjs/operators';
import {CaseReviewService} from '../../../@core/service/case-review/case-review.service';


interface DataListModel {
  name: string;
  value: string|number;
}
// 面包屑接口定义
interface Breadcrumb {
  title?: string; // 用于显示的名称
  params?: Params; // 跳转时所带参数
  url?: string; // 跳转的url
}


@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
  @ViewChild ('ngbPopover') Popover;
  @ViewChild ('popTemplate') PopoverTpl;
  private session = sessionStorage;
  public data: DataListModel[] = [
    {name: '新增线索', value: 6}, 
    {name: '新增案件', value: 0},
    {name: '审批通过', value: 8}, 
    {name: '到期提醒', value: 0},
  ];
  public html = `
    <ul class='mypopover-ul'>
      ${this.data.map((item) => {
        return `<li><a>${item.name}</a> ：${item.value}</li>`;
      }).join('')}
    </ul>
  `;
  username: string;
  // 面包屑数组
  public breadcrumbs: Breadcrumb[];
  constructor(
    private injector: Injector,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    public el: ElementRef,
    public caseReview: CaseReviewService,
    ) {
       // 设置面包屑
      this.router.events.pipe(filter((event) => event instanceof NavigationEnd))
      .subscribe((event) => {
        const root: ActivatedRoute = this.activatedRoute.root;
        const snapshot = this.router.routerState.snapshot;
        this.breadcrumbs = this.getBreadcrumbs(root);
      });
     }

  // 每次路由修改调用
  getBreadcrumbs(route: ActivatedRoute, url: string = '', breadcrumbs: Breadcrumb[] = []): Breadcrumb[] {
    const BREADCRUMB = 'breadcrumb';
    // 获取child路由
    const children: ActivatedRoute[] = route.children;
     //
    if (children.length === 0) {
      return breadcrumbs;
    }
    for (const child of children) {
      if (!child.snapshot.data.hasOwnProperty(BREADCRUMB)) {
        return this.getBreadcrumbs(child, url, breadcrumbs);
      }
      const routeURL: string = child.snapshot.url.map((segment) => segment.path).join('/');
      if (!routeURL) {
        return this.getBreadcrumbs(child, url, breadcrumbs);
      }
      url += `/${routeURL}`;
      const breadcrumb: Breadcrumb = {
        title: child.snapshot.data.title,
        params: child.snapshot.params,
        url: '/main/home' + url,
      };
      breadcrumbs.push(breadcrumb);
      return this.getBreadcrumbs(child, url, breadcrumbs);
    }
    return breadcrumbs;
  }
  
  ngOnInit() {
        this.username = window.sessionStorage.getItem('realName');
  }

  userInfo(): void {

  }
  ngOnChanges(changes: SimpleChanges): void {
    // Called before any other lifecycle hook. Use it to inject dependencies, but avoid any serious work here.
    // Add '${implements OnChanges}' to the class.

  }

  exit(): void {
    console.log('退出登录');
    this.session.clear();
    console.log(this.router);
    this.injector.get(Router).navigateByUrl('/login');
  }

  showTooltip() : void {

  }

  hideTooltip() : void {

  }
}
